package com.zhiwei.fast.fragments

import android.os.Bundle
import androidx.recyclerview.widget.*
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.SizeUtils
import com.zhiwei.fast.R
import com.zhiwei.fast.adapter.LoopAdapter
import com.zhiwei.fast.databinding.FragmentSweepstakesBinding
import com.zhiwei.fast.fragments.models.SweepstakesFragmentViewModel
import com.zhiwei.most.base.fragment.BaseVmVbFragment
import com.zhiwei.most.ext.nav
import com.zhiwei.most.ext.onClickRxView
import com.zhiwei.most.ext.views.setSwipePanel

/**
 * Create by zhiweizhu on 2022/2/26
 * 抽奖演示
 */
class SweepstakesFragmentDemo: BaseVmVbFragment<SweepstakesFragmentViewModel, FragmentSweepstakesBinding>() {

    private var mAdapter: LoopAdapter? = null
    private var mAdapter1: LoopAdapter? = null
    private var mAdapter2: LoopAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        with(mViewBind){
            with(mViewBind){

                recyclerView.run {
                    layoutManager = LinearLayoutManager(requireContext())
                    mAdapter = LoopAdapter(R.layout.layout_loop_item, mViewModel.loadDataForNow()).apply {
                        addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL).apply {
                            setDrawable(ResourceUtils.getDrawable(R.drawable.item_decoration_30))
                        })
                    }
                    adapter = mAdapter
                }
                LinearSnapHelper().attachToRecyclerView(recyclerView)



                recyclerView1.run {
                    layoutManager = LinearLayoutManager(requireContext())
                    mAdapter1 = LoopAdapter(R.layout.layout_loop_item, mViewModel.loadDataForNow()).apply {
                        addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL).apply {
                            setDrawable(ResourceUtils.getDrawable(R.drawable.item_decoration_30))
                        })
                    }
                    adapter = mAdapter1
                }
                LinearSnapHelper().attachToRecyclerView(recyclerView1)


                recyclerView2.run {
                    layoutManager = LinearLayoutManager(requireContext())
                    mAdapter2 = LoopAdapter(R.layout.layout_loop_item, mViewModel.loadDataForNow()).apply {
                        addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL).apply {
                            setDrawable(ResourceUtils.getDrawable(R.drawable.item_decoration_30))
                        })
                    }
                    adapter = mAdapter2
                }
                LinearSnapHelper().attachToRecyclerView(recyclerView2)
            }


            start.onClickRxView {
                recyclerView.start(50, SizeUtils.dp2px(110f))
                recyclerView1.start(50, SizeUtils.dp2px(110f))
                recyclerView2.start(50, SizeUtils.dp2px(130f))
            }

            stop.onClickRxView {
                recyclerView.stop()
                recyclerView1.stop()
                recyclerView2.stop()
            }

            swipePanel.setSwipePanel {
                nav().navigateUp()
            }
        }
    }



    override fun lazyLoadData() {
       // mViewModel.loadData()

        with(mViewBind){
            recyclerView.run {
                smoothScrollToPosition(3)
            }

            recyclerView1.run {
                smoothScrollToPosition(3)
            }

            recyclerView2.run {
                smoothScrollToPosition(3)
            }
        }
    }

    override fun createObserver() {
        with(mViewModel){
            getLoopLiveData().observe(viewLifecycleOwner){
                LogUtils.d("createObserver :${GsonUtils.toJson(it)}")
                //mAdapter?.setList(it[0])
            }
        }
    }
}